package com.driver.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.driver.enums.LogType;
import com.driver.enums.Valid;
import com.driver.pojo.entity.SetLog;
import com.driver.pojo.response.R;
import com.driver.pojo.vo.SetVo;
import com.driver.service.ISetLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/log")
@RequiredArgsConstructor
@Slf4j
public class SetLogController {

    private final ISetLogService setLogService;

    @PostMapping("/return")
    public R<Void> returnSet(@RequestBody SetVo setVo) {
        SetLog setLog = new SetLog();
        setLog.setType(LogType.RETURN);
        setLog.setMessage("操作时间：" + LocalDateTime.now() + "；操作方式：return；取消设置电压" + setVo.getVoltage() + "，取消设置频率" + setVo.getRate());
        setLogService.save(setLog);
        return R.success();
    }

    @PostMapping("/list")
    public R<List<SetLog>> list() {
        LambdaQueryWrapper<SetLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetLog::getIsValid, Valid.VALID)
                .orderByDesc(SetLog::getCreateTime);
        return R.success(setLogService.list(wrapper));
    }
}
